home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / plan / src / holmenu.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  219 lines

  1. /*
  2.  * Create and destroy the holiday popup. It is used for editing the
  3.  * ~/.holiday file. It also has an informative help text that explains
  4.  * the syntax. When the popup is removed, the holiday file is re-parsed.
  5.  *
  6.  *    destroy_holiday_popup()
  7.  *    create_holiday_popup()
  8.  */
  9.  
  10. #ifndef MIPS
  11. #include <stdlib.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <Xm/Xm.h>
  16. #include <Xm/DialogS.h>
  17. #include <Xm/Form.h>
  18. #include <Xm/LabelP.h>
  19. #include <Xm/LabelG.h>
  20. #include <Xm/PushBP.h>
  21. #include <Xm/PushBG.h>
  22. #include <Xm/ScrolledW.h>
  23. #include <Xm/Text.h>
  24. #include <Xm/Protocols.h>
  25. #include "cal.h"
  26.  
  27. static void        done_callback(), cancel_callback();
  28. extern void        help_callback();
  29. extern char        *mystrdup();
  30. extern char        *parse_holidays();
  31. extern char        *resolve_tilde();
  32. #ifdef MIPS
  33. extern char        *malloc();
  34. #endif
  35.  
  36. extern Display        *display;    /* everybody uses the same server */
  37. extern struct edit    edit;        /* info about entry being edited */
  38. extern struct list    *mainlist;    /* list of all schedule entries */
  39. extern int        curr_year;    /* year being displayed, since 1900 */
  40. extern struct mainmenu    mainmenu;    /* all important main window widgets */
  41.  
  42. static BOOL        have_shell;    /* message popup exists if TRUE */
  43. static Widget        shell;        /* popup menu shell */
  44. static Widget        text;        /* text widget */
  45.  
  46.  
  47. /*
  48.  * destroy a popup. Remove it from the screen, and destroy its widgets.
  49.  * It's too much trouble to keep them for next time.
  50.  */
  51.  
  52. destroy_holiday_popup()
  53. {
  54.     char        *string;    /* contents of text widget */
  55.     char        *errmsg;    /* holiday parser error */
  56.     FILE        *fp;        /* holiday file */
  57.  
  58.     if (have_shell) {
  59.         if (!(fp = fopen(resolve_tilde(HOLIDAY_PATH), "w")))
  60.             perror(HOLIDAY_PATH);
  61.         else {
  62.             string = XmTextGetString(text);
  63.             if (fwrite(string, strlen(string), 1, fp) != 1)
  64.                 perror(HOLIDAY_PATH);
  65.             XtFree(string);
  66.             fclose(fp);
  67.             if (errmsg = parse_holidays(-1, TRUE))
  68.                 create_error_popup(mainmenu.cal, 0, errmsg);
  69.             draw_calendar();
  70.             draw_year_calendar();
  71.             draw_week_calendar();
  72.             update_all_listmenus();
  73.         }
  74.         XtPopdown(shell);
  75.         XTDESTROYWIDGET(shell);
  76.         have_shell = FALSE;
  77.         confirm_new_entry();
  78.     }
  79. }
  80.  
  81.  
  82. /*
  83.  * create a holiday popup as a separate application shell.
  84.  */
  85.  
  86. create_holiday_popup()
  87. {
  88.     Widget            form, but, help;
  89.     Arg            args[20];
  90.     int            n;
  91.     Atom            closewindow;
  92.     FILE            *fp;
  93.  
  94.     destroy_holiday_popup();
  95.  
  96.     n = 0;
  97.     XtSetArg(args[n], XmNdeleteResponse,    XmDO_NOTHING);        n++;
  98.     XtSetArg(args[n], XmNiconic,        False);            n++;
  99.     shell = XtAppCreateShell("Holidays", "plan",
  100.             applicationShellWidgetClass, display, args, n);
  101. #    ifdef EDITRES
  102.     XtAddEventHandler(shell, (EventMask)0, TRUE, 
  103.              _XEditResCheckMessages, NULL);
  104. #    endif
  105.     set_icon(shell, 1);
  106.     form = XtCreateManagedWidget("dayform", xmFormWidgetClass,
  107.             shell, NULL, 0);
  108.     XtAddCallback(form, XmNhelpCallback, help_callback,
  109.                             (XtPointer)"holiday");
  110.  
  111.                             /*-- buttons --*/
  112.     n = 0;
  113.     XtSetArg(args[n], XmNbottomAttachment,    XmATTACH_FORM);        n++;
  114.     XtSetArg(args[n], XmNbottomOffset,    8);            n++;
  115.     XtSetArg(args[n], XmNrightAttachment,    XmATTACH_FORM);        n++;
  116.     XtSetArg(args[n], XmNrightOffset,    8);            n++;
  117.     XtSetArg(args[n], XmNwidth,        80);            n++;
  118.     but = XtCreateManagedWidget("Done", xmPushButtonWidgetClass,
  119.             form, args, n);
  120.     XtAddCallback(but, XmNactivateCallback, done_callback, (XtPointer)0);
  121.     XtAddCallback(but, XmNhelpCallback,     help_callback,
  122.                         (XtPointer)"holiday_done");
  123.  
  124.     n = 0;
  125.     XtSetArg(args[n], XmNbottomAttachment,    XmATTACH_FORM);        n++;
  126.     XtSetArg(args[n], XmNbottomOffset,    8);            n++;
  127.     XtSetArg(args[n], XmNrightAttachment,    XmATTACH_WIDGET);    n++;
  128.     XtSetArg(args[n], XmNrightWidget,    but);            n++;
  129.     XtSetArg(args[n], XmNrightOffset,    8);            n++;
  130.     XtSetArg(args[n], XmNwidth,        80);            n++;
  131.     help = XtCreateManagedWidget("Help", xmPushButtonWidgetClass,
  132.             form, args, n);
  133.     XtAddCallback(help, XmNactivateCallback,
  134.                     help_callback, (XtPointer)"holiday");
  135.     XtAddCallback(help, XmNhelpCallback,
  136.                     help_callback, (XtPointer)"holiday");
  137.  
  138.     n = 0;
  139.     XtSetArg(args[n], XmNbottomAttachment,    XmATTACH_FORM);        n++;
  140.     XtSetArg(args[n], XmNbottomOffset,    8);            n++;
  141.     XtSetArg(args[n], XmNrightAttachment,    XmATTACH_WIDGET);    n++;
  142.     XtSetArg(args[n], XmNrightWidget,    help);            n++;
  143.     XtSetArg(args[n], XmNrightOffset,    8);            n++;
  144.     XtSetArg(args[n], XmNwidth,        80);            n++;
  145.     but = XtCreateManagedWidget("Cancel", xmPushButtonWidgetClass,
  146.             form, args, n);
  147.     XtAddCallback(but, XmNactivateCallback, cancel_callback,(XtPointer)0);
  148.     XtAddCallback(but, XmNhelpCallback,     help_callback,
  149.                           (XtPointer)"holiday_cancel");
  150.  
  151.                             /*-- text --*/
  152.     n = 0;
  153.     XtSetArg(args[n], XmNtopAttachment,    XmATTACH_FORM);        n++;
  154.     XtSetArg(args[n], XmNtopOffset,        8);            n++;
  155.     XtSetArg(args[n], XmNbottomAttachment,    XmATTACH_WIDGET);    n++;
  156.     XtSetArg(args[n], XmNbottomWidget,    but);            n++;
  157.     XtSetArg(args[n], XmNbottomOffset,    16);            n++;
  158.     XtSetArg(args[n], XmNleftAttachment,    XmATTACH_FORM);        n++;
  159.     XtSetArg(args[n], XmNleftOffset,    8);            n++;
  160.     XtSetArg(args[n], XmNrightAttachment,    XmATTACH_FORM);        n++;
  161.     XtSetArg(args[n], XmNrightOffset,    8);            n++;
  162.     XtSetArg(args[n], XmNhighlightThickness,0);            n++;
  163.     XtSetArg(args[n], XmNeditMode,        XmMULTI_LINE_EDIT);    n++;
  164.     XtSetArg(args[n], XmNcolumns,        80);            n++;
  165.     XtSetArg(args[n], XmNrows,        20);            n++;
  166.     XtSetArg(args[n], XmNscrollVertical,    True);            n++;
  167.     XtSetArg(args[n], XmNpendingDelete,    False);            n++;
  168.     text = XmCreateScrolledText(form, "holiday", args, n);
  169.  
  170.     if (fp = fopen(resolve_tilde(HOLIDAY_PATH), "r")) {
  171.         fseek(fp, 0, 2);
  172.         if (n = ftell(fp)) {
  173.             char *buf;
  174.             rewind(fp);
  175.             if (buf = malloc(n+1)) {
  176.                 if (fread(buf, n, 1, fp) == 1) {
  177.                     buf[n] = 0;
  178.                     XmTextSetString(text, buf);
  179.                 }
  180.                 free(buf);
  181.             }
  182.         }
  183.         fclose(fp);
  184.     }
  185.     XmTextSetInsertionPosition(text, 0);
  186.     XtManageChild(text);
  187.     XtAddCallback(text, XmNhelpCallback, help_callback,
  188.                             (XtPointer)"holiday");
  189.  
  190.     XtPopup(shell, XtGrabNone);
  191.     closewindow = XmInternAtom(display, "WM_DELETE_WINDOW", False);
  192.     XmAddWMProtocolCallback(shell, closewindow,
  193.                     done_callback, (XtPointer)shell);
  194.  
  195.     have_shell = TRUE;
  196. }
  197.  
  198.  
  199. /*-------------------------------------------------- callbacks --------------*/
  200. /*ARGSUSED*/
  201. static void done_callback(widget, item, data)
  202.     Widget                widget;
  203.     int                item;
  204.     XmToggleButtonCallbackStruct    *data;
  205. {
  206.     destroy_holiday_popup();
  207. }
  208.  
  209. /*ARGSUSED*/
  210. static void cancel_callback(widget, item, data)
  211.     Widget                widget;
  212.     int                item;
  213.     XmToggleButtonCallbackStruct    *data;
  214. {
  215.     XtPopdown(shell);
  216.     XTDESTROYWIDGET(shell);
  217.     have_shell = FALSE;
  218. }
  219.